Fix loss of type inference in chained comparisons#21159
Closed
aryanjsingh wants to merge 1 commit intopython:masterfrom
Closed
Fix loss of type inference in chained comparisons#21159aryanjsingh wants to merge 1 commit intopython:masterfrom
aryanjsingh wants to merge 1 commit intopython:masterfrom
Conversation
A regression was introduced that caused loss of type inference for operands in chained comparisons, such as `None is not a == b`. The logic to prevent unwanted type widening was too aggressive and discarded valid narrowing from `is not` checks. The fix adjusts the condition in `update_from_options` to allow updates when only a single frame is being merged. This correctly identifies sequential updates within expressions (like chained comparisons) and preserves the narrowing, while still preventing unwanted widening when merging multiple control-flow branches.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Collaborator
|
Thanks, but this issue is already fixed on master. I also don't really understand this proposed fix. PRs to mypy should come with tests. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A regression in mypy 1.20 caused loss of type narrowing for operands in chained comparisons. This was due to an overly aggressive check in the type binder that prevented unwanted type widening but also discarded valid narrowing from expressions like
is not. The fix refines this check to distinguish between merging control-flow branches and sequential updates within an expression, ensuring that narrowing is preserved in chained comparisons.Changes
update_from_optionsmethod has been modified to correctly handle type narrowing in chained comparisons. The original logic was too aggressive in preventing type widening and inadvertently discarded valid narrowing. The fix introduces a check on the number of frames being merged: if only one frame is involved, it's treated as a sequential update (like in a chained comparison) and the type narrowing is allowed to proceed. This restores correct type inference without re-introducing the bug the original code was intended to fix.Related Issue
Closes #21149